import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:raze/artifacts/diary_deletion_dialog.dart';
import 'package:raze/artifacts/notifier_glimmer.dart';
import 'package:raze/lore/chronicle/diary_chronicle.dart';
import 'package:raze/potions/raze_constant.dart';
import 'package:raze/potions/spatial_alchemy.dart';

/// 日志详情页面
class DiaryDetailRealms extends StatelessWidget {
  const DiaryDetailRealms({super.key});

  @override
  Widget build(BuildContext context) {
    // 从路由参数获取日志ID
    final diaryId = Get.parameters['diaryId'] ?? '';
    final diaryChronicle = Get.find<DiaryChronicle>();

    // 获取日志详情
    final diary = diaryChronicle.queryDiaryById(diaryId);
    if (diary == null) {
      return Scaffold(
        backgroundColor: RazeConstants.background,
        body: Center(
          child:
              Text('Diary not found')
                  .applyTypeSize(16.sp)
                  .applyTypeColor(RazeConstants.black)
                  .applyNotoSans(),
        ),
      );
    }

    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/background.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: SafeArea(
          bottom: false,
          child: Column(
            children: [
              // 顶部导航栏
              _composeTopNavigationBar(diaryId, diaryChronicle),

              // 内容区域（可滚动）
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 图片部分
                      _constructImageSection(diary),

                      // 文字内容区域
                      _constructTextContentSection(diary),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 组装顶部导航栏
  Widget _composeTopNavigationBar(
    String diaryId,
    DiaryChronicle diaryChronicle,
  ) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // 返回按钮
          Container(
                width: 30.w,
                height: 30.w,
                decoration: BoxDecoration(
                  color: RazeConstants.background,
                  borderRadius: BorderRadius.circular(8.r),
                  border: Border.all(color: RazeConstants.black, width: 2.w),
                ),
                child: Icon(
                  Icons.chevron_left_rounded,
                  size: 24.sp,
                  color: RazeConstants.black,
                ),
              )
              .withRightBottomShadow(thickness: 1)
              .withTouchResonance(onTap: () => Get.back()),

          // 删除按钮
          Container(
                width: 30.w,
                height: 30.w,
                decoration: BoxDecoration(
                  color: RazeConstants.background,
                  borderRadius: BorderRadius.circular(8.r),
                  border: Border.all(color: RazeConstants.black, width: 2.w),
                ),
                padding: EdgeInsets.all(5.w),
                child: Image.asset('assets/images/delete.png'),
              )
              .withRightBottomShadow(thickness: 1)
              .withTouchResonance(
                onTap: () {
                  DiaryDeletionDialog.revealDeletionConfirmation(
                    onConfirm: () {
                      // 删除日志
                      final success = diaryChronicle.eraseDiaryById(diaryId);
                      if (success) {
                        NotifierGlimmer.revealInfo(
                          'Diary deleted successfully.',
                        );
                        Get.back();
                      } else {
                        NotifierGlimmer.revealInfo('Failed to delete diary.');
                      }
                    },
                  );
                },
              ),
        ],
      ),
    );
  }

  /// 构建图片部分
  Widget _constructImageSection(dynamic diary) {
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 12.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: RazeConstants.black, width: 1.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.r),
        child: AspectRatio(
          aspectRatio: 343 / 205, // 图片宽高比
          child:
              diary.contentType == 'video'
                  ? Stack(
                    fit: StackFit.expand,
                    children: [
                      // 视频封面
                      Image.asset(
                        diary.coverImage ?? diary.media,
                        fit: BoxFit.cover,
                      ),
                      // 播放图标
                      Center(
                        child: Container(
                          padding: EdgeInsets.all(12.w),
                          decoration: BoxDecoration(
                            color: RazeConstants.black.withOpacity(0.5),
                            shape: BoxShape.circle,
                          ),
                          child: Icon(
                            Icons.play_arrow_rounded,
                            size: 32.sp,
                            color: RazeConstants.white,
                          ),
                        ),
                      ),
                    ],
                  )
                  : Image.asset(diary.media, fit: BoxFit.cover),
        ),
      ),
    ).withRightBottomShadow(thickness: 1.5);
  }

  /// 构建文字内容区域
  Widget _constructTextContentSection(dynamic diary) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
      padding: EdgeInsets.all(20.w),
      constraints: BoxConstraints(minHeight: 470.h),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/diary-detail-background.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: Text(
        diary.detail,
        style: TextStyle(
          fontSize: 14.sp,
          color: RazeConstants.black,
          height: 1.50,
        ),
      ).applyNotoSans(weight: FontWeight.w400),
    );
  }
}
